home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / PROG_TOO / C023A.ZIP / PART2 / ZOPT5.C < prev    next >
Text File  |  1990-01-31  |  3KB  |  137 lines

  1. /*
  2.  * zopt - five pass optimiser for small-C  (fifth pass)
  3.  *        v2.0 - uses independent processes
  4.  *
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "zopt.h"
  10.  
  11. char *l1, *l2, *l3, *l4, *l5, *l6, *l7, *l8, *l9, *l10 ;
  12.  
  13. pass5()
  14. {
  15.     int saved[2] ;
  16.     int i ;
  17.     char *tail, *tail2, *temp ;
  18.  
  19.     saved[0] = saved[1] = 0 ;
  20.  
  21.     l1 = alloc(LINELEN) ;
  22.     l2 = alloc(LINELEN) ;
  23.     l3 = alloc(LINELEN) ;
  24.     l4 = alloc(LINELEN) ;
  25.     l5 = alloc(LINELEN) ;
  26.     l6 = alloc(LINELEN) ;
  27.     l7 = alloc(LINELEN) ;
  28.     l8 = alloc(LINELEN) ;
  29.     l9 = alloc(LINELEN) ;
  30.     l10 = alloc(LINELEN) ;
  31.  
  32.     p_read(l1) ;
  33.     p_read(l2) ;
  34.     p_read(l3) ;
  35.     p_read(l4) ;
  36.     p_read(l5) ;
  37.     p_read(l6) ;
  38.     p_read(l7) ;
  39.     p_read(l8) ;
  40.     p_read(l9) ;
  41.  
  42.     while ( p_read(l10) ) {
  43.  
  44.         /* JP Z around unconditional return */
  45.         saved[0] += jp_ret("\tJP Z,", "\tRET NZ") ;
  46.  
  47.         /* JP NZ around unconditional return */
  48.         saved[0] += jp_ret("\tJP NZ,", "\tRET Z") ;
  49.  
  50.         /* change small forward jump to relative jump */
  51.         if ( (tail=match("\tJP Z,", l1)) || (tail=match("\tJP NZ,", l1)) ||
  52.                     (tail=match("\tJP ", l1)) ) {
  53.           if ( (tail2=match(tail, l3)) || (tail2=match(tail, l4)) ||
  54.                 (tail2=match(tail, l5)) || (tail2=match(tail, l6)) ||
  55.                 (tail2=match(tail, l7)) || (tail2=match(tail, l8)) ||
  56.                 (tail2=match(tail, l9)) || (tail2=match(tail, l10)) ) {
  57.             if ( tail2[0] == ':' ) {
  58.               l1[2] = 'R' ;
  59.               ++saved[1] ;
  60.             }
  61.           }
  62.         }
  63.  
  64.         /* change small backward jump to relative jump */
  65.         if ( (tail=match("\tJP Z,", l10)) || (tail=match("\tJP NZ,", l10)) ||
  66.                     (tail=match("\tJP ", l10)) ) {
  67.           if ( (tail2=match(tail, l8)) || (tail2=match(tail, l7)) ||
  68.                 (tail2=match(tail, l6)) || (tail2=match(tail, l5)) ||
  69.                 (tail2=match(tail, l4)) || (tail2=match(tail, l3)) ||
  70.                 (tail2=match(tail, l2)) || (tail2=match(tail, l1)) ) {
  71.             if ( tail2[0] == ':' ) {
  72.               l10[2] = 'R' ;
  73.               ++saved[1] ;
  74.             }
  75.           }
  76.         }
  77.  
  78.         putline(l1);    
  79.         temp = l1;
  80.         l1 = l2;
  81.         l2 = l3;
  82.         l3 = l4;
  83.         l4 = l5;
  84.         l5 = l6;
  85.         l6 = l7;
  86.         l7 = l8;
  87.         l8 = l9;
  88.         l9 = l10;
  89.         l10 = temp;
  90.         if (cpm(CONIN, 255) == CTRLC) exit() ;
  91.     }
  92.     putline(l1);
  93.     putline(l2);
  94.     putline(l3);
  95.     putline(l4);
  96.     putline(l5);
  97.     putline(l6);
  98.     putline(l7);
  99.     putline(l8);
  100.     putline(l9);
  101.  
  102.     puts("Cond JP round uncond RET      "); putdec(saved[0]) ;
  103.     putchar('\n') ;
  104.     puts("Relative jump                 "); putdec(saved[1]) ;
  105.     putchar('\n') ;
  106.     putchar('\n') ;
  107.     i = saved[0]*3 + saved[1] ;
  108.     pr_total(i);
  109.  
  110.     Total += i ;
  111. }
  112.  
  113. /*
  114.  * change conditional jump around uncontional return
  115.  * to conditional return
  116.  * return 1 if substitution made, else 0
  117.  */
  118. jp_ret(jump, rtn)
  119. char *jump, *rtn ;
  120. {
  121.     char *temp, *tail, *tail2 ;
  122.  
  123.     if ( (tail=match(jump, l8)) ) {
  124.       if ( (tail2=match(tail, l10)) && tail2[0] == ':' ) {
  125.         if ( strcmp(Ret, l9) == 0 ) {
  126.           strcpy(l8, rtn) ;
  127.           temp = l9 ;
  128.           l9 = l10 ;
  129.           l10 = temp ;
  130.           p_read(l10) ;
  131.           return 1 ;
  132.         }
  133.       }
  134.     }
  135.     return 0 ;
  136. }
  137.